From ea2c6e8afe3262d5ed8a65220e07e9d347bd465a Mon Sep 17 00:00:00 2001 From: Paul Woolcock Date: Thu, 1 Dec 2016 22:42:45 -0500 Subject: [PATCH] Assume build = 'build.rs' by default This change makes cargo assume `build = "build.rs"` if there is a `build.rs` file in the same directory as `Cargo.toml`. However, you can set `build = false` to prevent this. --- src/cargo/util/toml.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 5e5dc93a6..1d4f8608a 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -286,12 +286,18 @@ pub struct TomlProfile { panic: Option, } +#[derive(RustcDecodable, Clone, Debug)] +pub enum StringOrBool { + String(String), + Bool(bool), +} + #[derive(RustcDecodable)] pub struct TomlProject { name: String, version: TomlVersion, authors: Vec, - build: Option, + build: Option, links: Option, exclude: Option>, include: Option>, @@ -540,7 +546,11 @@ impl TomlManifest { } // processing the custom build script - let new_build = project.build.as_ref().map(PathBuf::from); + let manifest_file = util::important_paths::find_root_manifest_for_wd(None, &layout.root) + .chain_error(|| human("Could not find root manifest location"))?; + let base_dir = manifest_file.parent() + .ok_or(human("Could not get parent directory of manifest"))?; + let new_build = self.maybe_custom_build(&project.build, &base_dir); // Get targets let targets = normalize(&lib, @@ -767,6 +777,23 @@ impl TomlManifest { } Ok(replace) } + + fn maybe_custom_build(&self, build: &Option, project_dir: &Path) + -> Option { + let build_rs = project_dir.join("build.rs"); + match *build { + Some(StringOrBool::Bool(false)) => None, // explicitly no build script + Some(StringOrBool::Bool(true)) => Some(build_rs.into()), + Some(StringOrBool::String(ref s)) => Some(PathBuf::from(s)), + None => { + match fs::metadata(&build_rs) { + Ok(ref e) if e.is_file() => Some(build_rs.into()), + Ok(_) => None, + Err(_) => None, + } + } + } + } } /// Will check a list of toml targets, and make sure the target names are unique within a vector. -- 2.30.2